<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of Pager
 *
 * @author Андрей
 */
class Searcher {

    private $fields = array();

    private $input_criteria = array();
    
    private $where_sql = '';
    
    public function __construct($fields, $input_criteria) {
        $this->fields = $fields;
        $this->input_criteria = $input_criteria;
        
        $this->generateWhereSQL();
    }
    
    private function generateWhereSQL() {

        $total_fields = count($this->fields);
        $query = '';
        foreach ($this->fields as $id => $field) {
            if (empty($this->input_criteria[$field['field']])) {
                $total_fields--;
                continue;
            }
            switch ($field['type']) {
                case 'match':

                    $query.='MATCH(' . $field['field'] . ') AGAINST (\'' . $this->input_criteria[$field['field']] . '\')';

                    break;

                default:
                    $query.= $field['field'] . '=\'' . $this->input_criteria[$field['field']] . '\'';
                    break;
            }

            if ($id < $total_fields-1)
                $query.=' AND ';
        }

        if(!empty($query))
        {
            $query = 'WHERE '.$query;
            $this->where_sql = $query;
        }

    }


    public function getWhereSQL()
    {
        return $this->where_sql;
    }
}


?>
